/***尚硅谷Java数据结构与java算法                   ***/
/***https://www.bilibili.com/video/BV1E4411H73v***/
/***单向链表，不考虑编号的排序     P18-24          ***/
/***C++ 实现版本    written by Daniel           ***/
#include <string>
#include <sstream>
#include <iostream>

using std::string;
class HeroNode
{
public:
    HeroNode(int hNum, string hName, string hNickName)
    {
        this->num = hNum;
        this->name = hName;
        this->nickName = hNickName;
    }
    string toString()
    {
        return "Hero [No=" + std::to_string(num) +"\t"+ "name=" + name + +"\t""nickname=" + nickName + "]";
    }
    int num;
    string name;
    string nickName;
    HeroNode *next=nullptr;

private:
};

class HeroNodeLinkedList
{
public:
    void addNodeByOrder(HeroNode* heroNode)
    {
        HeroNode* temp;
        temp=head;
        while(temp->next!=nullptr){
            temp=temp->next;
        }
        temp->next=heroNode;
    }

    void Print()
    {
        HeroNode* temp;
        temp=this->head;
        while(temp!=nullptr){
            std::cout<<temp->toString()<<std::endl;
            temp=temp->next;
        }
        return;
    }
    ~HeroNodeLinkedList()
    {
        std::cout<<"析构链表"<<std::endl;
        HeroNode* temp=this->head;
        HeroNode* tempToFree;
        while(temp->next!=nullptr){
            tempToFree=temp;
            temp=temp->next;
            delete tempToFree;
            if (tempToFree!=nullptr)
                tempToFree=nullptr;
        }
        delete temp;
        if (temp!=nullptr)
            temp=nullptr;
        if (temp==nullptr){
            std::cout<<"析构成功"<<std::endl;
        }
    }
private:
    HeroNode* head=new HeroNode(0,"","");

};

int main(int argc, char **argv)
{
    HeroNode* hero1=new HeroNode(1,"宋江","及时雨");
    HeroNode* hero2=new HeroNode(2,"吴用","智多星");
    HeroNode* hero3=new HeroNode(3,"林冲","豹子头");
    HeroNodeLinkedList* heroList=new HeroNodeLinkedList();
    heroList->addNodeByOrder(hero1);
    heroList->addNodeByOrder(hero2);
    heroList->addNodeByOrder(hero3);
    heroList->Print();
    if (heroList!=nullptr){
        delete heroList;
        heroList=nullptr;
    }
    return 0;
}